using System;
using System.Collections.Generic;
using System.Text;
using System.IO;

namespace Wootbar
{
    public static class KeyHelper
    {
        private static string GenerateComputerKey()
        {
            try
            {
                long Key = System.Environment.TickCount + DateTime.Now.Ticks;
                return Key.ToString();
            }
            catch (Exception){throw;}            
        }

        public static string GetKey(string KeyPath)
        {
            if (!File.Exists(KeyPath))
            {               
                string key =  GenerateComputerKey();
                WriteComputerKey(KeyPath, key);
                return key;
            }
            else
            {
                return ReadComputerKey(KeyPath);
            }

        }
        private static void WriteComputerKey(string KeyPath, string Key)
        {
            try
            {
                if (!File.Exists(KeyPath))
                {
                    using (StreamWriter sw = new StreamWriter(KeyPath))
                    {
                        sw.WriteLine(Key.ToString());                
                    }
                }                
            }
            catch (Exception){throw;}
        }

        private static string ReadComputerKey(string KeyPath)
        {
            try
            {                
                if (File.Exists(KeyPath))
                {
                    using (StreamReader sr = new StreamReader(KeyPath))
                    {
                        return sr.ReadLine();
                    }
                }
                return string.Empty;
            }
            catch (Exception){throw;}
        }
    }    
}
